<?php
class Admin_SupportController extends Apps_Controller_Action_Admin {

	public function indexAction() {
		$this->_helper->redirector('read');
	}

	public function readAction() {
	    $dbAdapter = Zend_Db_Table::getDefaultAdapter();
	    $select = $dbAdapter
            	    ->select()
            	    ->from('support', null)
            	    ->columns(array(
            	            'id'             => 'support.id',
            	            'supporter_name' => 'support.supporter_name',
            	            'account'         => 'support.account',
            	            'phone'         => 'support.phone',
            	            'sorting_number'  => 'support.sorting_number',
            	            'type'         => new Zend_Db_Expr('CASE support.type
                        	                    WHEN '. Model_DbTable_Support::TYPE_YAHOO .' THEN "Yahoo"
                        	                    WHEN '. Model_DbTable_Support::TYPE_SKYPE .' THEN "Skype"
                        	                    END'),
            	            'status' => 'support.status',
            	            'status_label' => new Zend_Db_Expr('CASE support.status WHEN 1 THEN "Yes" WHEN 0 THEN "No" END'),
            	    ));

	    $grid = new Grid_Support_Support('grid-support');
	    $grid->setCaption('List Supporter');
	    $grid->setSource(new OeZend_Widget_Grid_Source_ZendDbSelect($select));
	    $grid->setView(new OeZend_Widget_Grid_View());
	    $grid->setDisablePaginator();

	    $grid->deploy();

	    $this->view->grid = $grid;
	}

	public function createAction() {
		$rq = $this->_request;

		$tbSupport = new Model_DbTable_Support();
		$f 	= new Admin_Form_AddSupport();

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbSupport->insert($f->getValues())
					? self::$msgAddSuccess
					: self::$msgAddFailed);

				$this->_helper->redirector('read', null, null);
			}
		}
		$this->view->form = $f;
	}

	public function updateAction() {
		$rq = $this->_request;

		$supportId = intval($rq->getParam('id'));
		$tbSupport = new Model_DbTable_Support();
		$f = new Admin_Form_AddSupport();

		if(!$supportId || !($r = $tbSupport->fetchRow(array('id = ?' => $supportId))->toArray())) {
			$this->_flashMessenger->addMessage(self::$msgNoRecordFound);
			$this->goToActionIndex();
		}

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$this->_flashMessenger->addMessage(
					$tbSupport->safelyUpdate($f->getValues(), array('id = ?' => $supportId))
					? self::$msgAddSuccess
					: self::$msgAddFailed);

				$this->_helper->redirector('read', null, null);
			}
		} else {
			$f->populate($r);
		}
		$f->setLegend('Edit Suppoter');
		$this->view->form = $f;
		$this->render('create');
	}

	public function deleteAction() {
		$this->disableLayout(true, true);

		$rq = $this->_request;
		$supportIds = $rq->getParam('id');
		$tbSupport = new Model_DbTable_Support();

		if(is_numeric($supportIds)) {
			$this->_flashMessenger->addMessage(
						$tbSupport->safelyDelete(array('id = ?' => $supportIds)) ?
						self::$msgDeleteSuccess :
						self::$msgDeleteError
					);
			$this->goToActionIndex();
		} elseif (is_array($supportIds)) {
			foreach ($supportIds as $faqId) {
				$tbSupport->safelyDelete(array('id = ?' => $faqId));
			}
			$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
			$this->goToActionIndex();
		} else {
			$this->_flashMessenger->addMessage(self::$msgNoRecordFound);
			$this->goToActionIndex();
		}
	}

	public function changeValueAction() {
	    $this->disableLayout(true, true);
	    $rq = $this->_request;
	    $param  = $rq->getParam('p');
	    $id 	= intval($rq->getParam('id'));
	    $tbSupport = new Model_DbTable_Support();

	    if(!($r = $tbSupport->fetchRow(array('id = ?' => $id))->toArray())) {
	        echo '<script>window.location=window.location</script>
	        <a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
	        . $this->getBacktrackUrl()
	        . '">Update Error (Record not found), please wait ...</a>';
	    }

	    switch ($param) {
	        case 'public':
	            ($r['status'] == Model_DbTable_Support::STATUS_ACTIVE) ?
	            $tbSupport->safelyUpdate(array('status'  => Model_DbTable_Support::STATUS_INACTIVE),
	            array('id = ?'	  => $id)) :
	            $tbSupport->safelyUpdate(array('status'  => Model_DbTable_Support::STATUS_ACTIVE),
	            array('id = ?'  => $id));
	            break;
	        default: break;
	    }

	    echo '<script>document.location.reload()</script>
	    <a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
	    . $this->getBacktrackUrl()
	    . '">Update successfully, please wait ...</a>';
	}



}